import annation.Colunm;
import annation.Key;
import annation.Table;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.*;

import org.apache.poi.ss.formula.functions.T;
import utils.ReflectUtils;

/**
 * @author zsy
 * @Description: excel 转为 object 工厂
 * @create 2018-01-04 13:17
 **/
public class ObjectFactory {

    public <T> Map<Object,T> load(Class<T> classOfT) {
        List<T> list = new ArrayList<>();
        //使用@key注解的字段为key,实体类是value
        Map<Object, T> objectMap = new HashMap<>();
        try {
            Table table = classOfT.getAnnotation(Table.class);
            String file = table.fileName();
            String sheetName = table.sheetName();

            for (Map<String, Object> map : ExcelUtils.readExcel2Map(file, sheetName)) {
                T t = classOfT.newInstance();
                Object key = null;
                for (Field field : t.getClass().getDeclaredFields()) {
                    field.setAccessible(true);
                    Colunm colunm = field.getAnnotation(Colunm.class);
                    String lableName = colunm.name();

                    if (List.class.isAssignableFrom(field.getType())) {
                        //元素的类型
                        Type elementType = ReflectUtils.getGenericType(field)[0];
                        //元素的类型名字
                        String elementClassName = ReflectUtils.getClassName(elementType);
                        //表格字符串内容
                        String cellValue = (String) map.get(lableName);
                        field.set(t, getListByClssName(elementClassName, cellValue));
                    } else {
                        Object value = map.get(lableName);
                        if (field.getType().getName().equals("int") && value instanceof Double) {
                            value = ((Double) value).intValue();
                            field.set(t, value);
                        } else {
                            field.set(t, value);
                        }
                        //key只能是基本类型
                        if (field.isAnnotationPresent(Key.class)) {
                            key = value;
                        }
                    }
                }
                list.add(t);
                objectMap.put(key, t);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return objectMap;
    }

    /**
     * @Description: 创建一个指定类型的List
     * @Author: zsy
     * @Date: 2018/1/5 13:40
     */
    private List getListByClssName(String className, String cellValue) throws Exception {
        String[] strings = cellValue.split(",");

        if (className.contains("Integer")) {
            List<Integer> list = new ArrayList<>();
            for (String s : strings) {
                list.add(Double.valueOf(s).intValue());
            }
            return list;
        } else if (className.contains("String")) {
            List<String> list = new ArrayList<>();
            Collections.addAll(list, strings);
            return list;
        } else if (className.contains("Double")) {
            List<Double> list = new ArrayList<>();
            for (String s : strings) {
                list.add(Double.valueOf(s));
            }
            return list;
        } else {
            throw new Exception("不支持 List<" + className + "> 类型");
        }
    }

}
